package com.cmwa.scbp.sys.msg.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cmwa.scbp.base.mvc.entity.Page;
import com.cmwa.scbp.base.mvc.entity.SearchParam;
import com.cmwa.scbp.sys.msg.service.IMsgPublicEmailService;
import com.cmwa.scbp.sys.msg.vo.MsgPublicEmailVo;
import com.cmwa.scbp.base.constants.Constant;
import com.cmwa.scbp.utils.SessionUtils;

@Controller
@RequestMapping("/msgpublicemail")
public class MsgPublicEmailController {
	@Resource
	private IMsgPublicEmailService msgPublicEmailService;
	
	@RequestMapping("/gotoSelectPublicEmail")
	public String gotoSelectPublicEmail(@RequestParam("confId") String confId,@RequestParam("type") String type,ModelMap model){
		List<MsgPublicEmailVo> allEmails=msgPublicEmailService.queryAllPublicEmail();
		Map<String, String> params=new HashMap<String, String>();
		params.put("CONF_ID", confId);
		params.put("EMAIL_TYPE", type);
		List<MsgPublicEmailVo> selectEmails=msgPublicEmailService.queryPublicEmailByConfId(params);
		
		List<MsgPublicEmailVo> notContainsEmails=new ArrayList<MsgPublicEmailVo>();
		if(allEmails!=null && allEmails.size()>0){
			for(MsgPublicEmailVo one : allEmails){
				int i=0;
				if(selectEmails!=null && selectEmails.size()>0){
					for(MsgPublicEmailVo one2 : selectEmails){
						if(one.getId().equalsIgnoreCase(one2.getId())){
							i++;
							break;
						}
					}
				}
				if(i==0){
					notContainsEmails.add(one);
				}
			}
		}
		
		model.addAttribute("allEmails", allEmails);
		model.addAttribute("allEmails_option", parseString(allEmails));
		model.addAttribute("notContainsEmails", notContainsEmails);
		model.addAttribute("notContainsEmails_option", parseString(notContainsEmails));
		model.addAttribute("selectEmails", selectEmails);
		model.addAttribute("selectEmails_option", parseString(selectEmails));
		
		model.addAttribute("confId", confId);
		model.addAttribute("type", type);
		
		return "sys/msg/selectPublicEmail";
	}
	
	/**
	 * 
	 * @param emailName
	 * @return
	 * @author ex-zhangjia
	 */
	@RequestMapping("/selectPublicEmail")
	@ResponseBody
	public List<MsgPublicEmailVo> selectPublicEmail(@RequestParam(value = "emailName")String emailName){
		List<MsgPublicEmailVo> allEmails=new ArrayList<MsgPublicEmailVo>();
		SearchParam sp=new SearchParam();
		sp.getSp().put("emailName", emailName);
		allEmails=msgPublicEmailService.queryAllPublicEmailByName(sp);
		return allEmails;
	}
	
	public String parseString(List<MsgPublicEmailVo> res){
		if(res==null || res.size()==0){
			return "";
		}
		StringBuffer sb=new StringBuffer("");
		for(MsgPublicEmailVo one : res){
			sb.append("<option value='");
			sb.append(one.getId());
			sb.append("'>");
			sb.append(one.getEmailName());
			sb.append("</option>");
		}
		
		
		return sb.toString();
	}
	
	@RequestMapping("/publicEmailListView")
	public String publicEmailListView() {
		return "system/sys/msg/publicEmailList";
	}
	
	@RequestMapping("/publicEmailListPage")
	@ResponseBody
	public Object publicEmailListPage(SearchParam sp) {
		Page<MsgPublicEmailVo> page = msgPublicEmailService.publicEmailListPage(sp);
		return page;
	}
	
	@RequestMapping("publicEmailAddView")
	public String publicEmailAddView() {
		return "system/sys/msg/publicEmailAdd";
	}
	
	@RequestMapping("publicEmailUpdateView")
	public String publicEmailUpdateView(@RequestParam("id")String id, Model model) {
		model.addAttribute("vo", msgPublicEmailService.queryById(id));
		return "system/sys/msg/publicEmailUpdate";
	}
	
	@RequestMapping("add")
	public String add(MsgPublicEmailVo vo) {
		vo.setStatus(Constant.C_STATUS_C);
		vo.setCreateId(SessionUtils.getEmployee().getID());
		msgPublicEmailService.create(vo);
		return "system/hint/success";
	}

	@RequestMapping("update")
	public String update(MsgPublicEmailVo vo) {
		vo.setStatus(Constant.C_STATUS_C);
		vo.setCreateId(SessionUtils.getEmployee().getID());
		msgPublicEmailService.update(vo);
		return "system/hint/success";
	}
}
